package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Shipping;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IShippingService;

@Controller
@RequestMapping("/order")
public class FrontOrderController {
	@Autowired
	private IShippingService shippingService;
	@Autowired
	private ICartService cartService;
	@Autowired
	private IOrderService orderService;
	
	@RequestMapping("/getOrderConfirmPage.shtml")
	public String getOrderConfirmPage(HttpSession session,Model model) {
		User user=(User) session.getAttribute("user");
		//当前用户收货地址信息.
		List<Shipping>shippingList=shippingService.selectByUserId(user.getId());
		model.addAttribute("shippingList", shippingList);
		//当前用户购物车里面"选中"要去结算生成订单的购物车列表
		List<Cart> cartList=cartService.selectByUserIdAndChecked(user.getId());
		model.addAttribute("cartList", cartList);
		return "order_confirm";
	}
	
	@RequestMapping("/insert.shtml")
	@ResponseBody
	public Result insert(Order order,HttpSession session) {
		User user=(User) session.getAttribute("user");
		order.setUserId(user.getId());
		boolean isSuccess=orderService.insert(order);
		if(isSuccess) {
			return Result.success("生成订单成功");
		}else {
			return Result.error("生成订单失败");
		}
		
	}
	
	@RequestMapping("/getOrderListPage.shtml")
	public String getOrderListPage(HttpSession session , Model model) {
		User user=(User) session.getAttribute("user");
		List<Order> list=orderService.selectByUserId(user.getId());
		model.addAttribute("list", list);
		return "order_list";
	}
	
	
}
